library Wave13a initializer init requires LegacySystem

    private function Wave13a takes nothing returns nothing
        local integer i = 1
        local integer A = 0
        local integer Cocoons = IMinBJ(udg_Players,4)
        local unit u
        set udg_CurrentWave = 13
        set udg_Visibility = 65
        call IntermissionRestore()
        set udg_Visibility = 67
        call IntermissionRestore()
        call GenericActions.execute()
        call SetRevelationSpawnEnabled(false)
        if Cocoons<=1 then
            call GeneralText(8.0, "|cffcd950cIron Hand|r : As if things weren't bad enough, FACs spotted a Cocoon in your sector." )
        else
            call GeneralText(8.0, "|cffcd950cIron Hand|r : As if things weren't bad enough, FACs spotted " + I2S(Cocoons) + " Cocoons in your sector." )
        endif
        call PolledWait(3.)
        set i = GetRandomInt(1, udg_Players)
        call FieldChat(udg_Hero[udg_Sorted[i]], 5, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r : Cocoons? Sonofabitch!" )
        call PolledWait(3.)
        call GeneralText(8.0, "|cffcd950cBattalion|r : They are at the final stage of the hatching cycle and whatever's inside looks hostile." )
        call PolledWait(4.)
        set udg_Visibility = 69
        call IntermissionRestore()
        set i = GetRandomInt(1, udg_Players)
        call FieldChat(udg_Hero[udg_Sorted[i]], 10, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r : The only day I'm not wearing my lucky boots, and shit hits the fan. Big time." )
        call PolledWait(3.)
        call GeneralText(8.0, "|cffcd950cBattalion|r : Patching in their last spotted coordinates. Destroy those cocoons before they hatch." )
        call PolledWait(6.)
        set i = 1
        loop
            exitwhen i>Cocoons
            set udg_Cocoon[i] = CreateUnit(Player(13), 'e00H', GetRandomReal(GetRectMinX(udg_PlayerSpawnBoundary), GetRectMaxX(udg_PlayerSpawnBoundary)),GetRandomReal(GetRectMinY(udg_PlayerSpawnBoundary), GetRectMaxY(udg_PlayerSpawnBoundary)),0.0)
            if udg_SquadXP>70000 and GetRandomInt(0,1)==1 then
                //No Cocoon Ping
            elseif udg_SquadXP>40000 and GetRandomInt(0,2)==1 then
                //No Cocoon Ping
            else
                call PingMinimapEx(GetUnitX(udg_Cocoon[i])+GetRandomReal(-500.0,500.0), GetUnitY(udg_Cocoon[i])+GetRandomReal(-500.0,500.0), 180.00, 255, 0, 0, false)
            endif
            set i=i+1
        endloop
        call RandomSpawn('u005',1,GetRandomInt(1,udg_Players)) //Gargolye #2
        call SpawnRectRandom(gg_rct_SpawnEastBottom,Player(13),'n003',udg_Spawn)
        call PolledWait(10.)
        if Cocoons<=1 then
            call MBSet( udg_SquadStatus, 1, 1, "Objective : Destroy the Cocoon" )
            call QuestSetDescription(udg_CurrentOrders, "S-2 believes the cocoon will mature in a matter of minutes, based on studies of satellite images. Swiftly eliminate it before it matures. The cocoon has thick carapace and the use of explosive charges is recommended." )
            call ALICE2Text(5.0, "|cff8b864eNew Objective|r : Search and Destroy the Cocoon." )
        else
            call MBSet( udg_SquadStatus, 1, 1, "Objective : Destroy the Cocoons" )
            call QuestSetDescription(udg_CurrentOrders, "S-2 believes the cocoons will mature in a matter of minutes, based on studies of satellite images. Swiftly eliminate them before they mature. The cocoons have thick carapace and the use of explosive charges is recommended." )
            call ALICE2Text(5.0, "|cff8b864eNew Objective|r : Search and Destroy the Cocoons." )
        endif
        call MissionUpdate.execute()
        call SpawnRectRandom(gg_rct_SpawnNorthWaterfall,Player(14),'n004',2*udg_Spawn)
        if getBoolean(null,"FOPresent") and udg_Players>2 then
            call GeneralText(6.0, "|cffcd950cMarine|r : Call Fire Missions at their coordinates. We have enough on our hands as it is." )
        endif
        call SpawnRectRandom(gg_rct_SpawnTownWestCave,Player(13),'n004',2*udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnSouthWaterfall,Player(14),'n00M',udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnSouthWaterfall,Player(13),'n003',udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnWestTop,Player(14),'n004',udg_Spawn)
        call PolledWait(60.)
        set i = GetRandomInt(1, udg_Players)
        call FieldChat(udg_Hero[udg_Sorted[i]], 5, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r : We need to split up. We're short on time." )
        call LightningAmbience.execute()
        call GankMarine(Player(13),'n004',2+GetRandomInt(1,udg_Spawn),1600,2000)
        call GankMarine(Player(14),'n00M',2+GetRandomInt(1,udg_Spawn),1600,2000)
        call PolledWait(60.)
        if udg_Mode==2 then
            call GeneralText(5.0, "|cfff3ad00Major Grimes|r : You've got two more minutes to take the cocoons out." )
        endif
        call GankMarine(Player(13),'n004',2+GetRandomInt(1,udg_Spawn),1600,2000)
        call GankMarine(Player(14),'n00D',2+udg_Spawn,1600,2000)
        call PolledWait(60.0*udg_Pace)
        call GankMarine(Player(13),'n004',1+GetRandomInt(1,udg_Spawn),1600,2000)
        call PolledWait(60.0*udg_Pace)
        set i = 1
        loop
            exitwhen i>Cocoons
            if GetWidgetLife(udg_Cocoon[i])>0.405 then
                set u = CreateUnit(Player(14),'U00K',GetUnitX(udg_Cocoon[i]),GetUnitY(udg_Cocoon[i]),0.0)
                call KillUnit(udg_Cocoon[i])
                call SuspendHeroXP(u,true)
                call TriggerSleepAction(0.)
                set A=A+1
            endif
            set i=i+1
        endloop
        if A==0 and udg_Mode==1 then
            call ALICEText(10.0, "All Cocoons neutralized. |cff1e90ff75 XP|r awarded to all Marines." )
            set i = 1
            loop
                exitwhen i>udg_InitialPlayers
                if GetPlayerSlotState(ConvertedPlayer(udg_Sorted[i]))!=PLAYER_SLOT_STATE_LEFT then
                    set udg_Experience[udg_Sorted[i]] = udg_Experience[udg_Sorted[i]] + 75
                endif
                set i=i+1
            endloop
        elseif A==1 then
            call ALICEText(10.0, "|cffcd950cALICE|r : A cocoon has matured." )
        elseif A>1 then
            call ALICEText(10.0, "|cffcd950cALICE|r : " + I2S(A) + " cocoons have matured." )
        endif
        call SetRevelationSpawnEnabled(true)
        if udg_Players>0 then
            if GetRandomInt(0,1)==1 and udg_SquadXP>20000 then
                call ExecuteRegisteredFunction("Wave14a2")
                //call Wave14f.execute()
            else
                call ExecuteRegisteredFunction("Wave14a1")
                //call Wave14a.execute()
            endif
        endif
        set u = null
    endfunction

    
    private function init takes nothing returns nothing
        call RegisterFunction("Wave13a", function Wave13a)
        // call ExecuteRegisteredFunction("Wave13a")
    endfunction
    
endlibrary

